public class Solution6 {
    //978. 最长湍流子数组
    public int maxTurbulenceSize(int[] nums) {
        int n = nums.length;
        int[] f = new int[n]; // 以上升趋势
        int[] g = new int[n]; //以下降趋势
        for (int i = 0; i < n; i++) {
            f[i] = g[i] = 1;
        }
        int ret = 1;
        for (int i = 1; i < n; i++) {
            if (nums[i] > nums[i-1]) {
                f[i] = g[i-1] + 1;
            } else if (nums[i] < nums[i-1]) {
                g[i] = f[i-1] + 1;
            }
            ret = Math.max(ret,Math.max(f[i],g[i]));
        }

        return ret;
    }
}
